
import ComponentBase from "../../../engine/ComponentBase";
import { Logger } from "../../../engine/Logger";
import UIManager from "../../../engine/UIManager";
import { ClubSocketClient } from "../../../net/ClubSocketClient";
import { ClubConfig } from "../../config/ClubConfig";
import { ViewConfig } from "../../config/ViewConfig";
import { UserModel } from "../../model/UserModel";
import { UIHelper } from "../../util/UIHelper";
import { ViewCheck } from "../ViewCheck";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewClubControlSetting extends ComponentBase {

    private btnDayang: cc.Node
    private btnKaiye: cc.Node
    private toggleSeemeber: cc.Toggle
    // private toggleSeerank: cc.Toggle;
    private limit_seerank:cc.ToggleContainer
    private limit_joinToggle: cc.ToggleContainer

    onEnable(): void {
        super.onEnable()
        this.btnDayang = this.findChild(this.node, "btnDayang")
        this.btnKaiye = this.findChild(this.node, "btnKaiye")
        this.toggleSeemeber = this.getChild(this.node, "toggleSeemeber", cc.Toggle);
        // this.toggleSeerank = this.getChild(this.node, "toggleSeerank", cc.Toggle);
        this.limit_seerank = this.getChild(this.node, "limit_seerank", cc.ToggleContainer);
        this.limit_joinToggle = this.getChild(this.node, "limit_joinToggle", cc.ToggleContainer)
        UIHelper.onAddEvent(this.btnDayang, () => {
            UIHelper.showCheck("确定亲友圈打烊吗?", (isSuc: boolean) => {
                if (isSuc) {
                    ClubSocketClient.req_club_close(UserModel.instance.nowClub.clubid, () => {
                        UIHelper.showCheck("亲友圈已打烊,成员不能进行游戏，正在游戏中的牌桌不受影响")
                        UserModel.instance.nowClub.state = ClubConfig.Club_State_Type.dayang
                        this.refresh();
                    })
                }
            }, ViewCheck.Dialog_Type.All)
        })
        UIHelper.onAddEvent(this.btnKaiye, () => {
            UIHelper.showCheck("确定亲友圈开始营业吗?", (isSuc: boolean) => {
                if (isSuc) {
                    ClubSocketClient.req_club_open(UserModel.instance.nowClub.clubid, () => {
                        UIHelper.showCheck("亲友圈已开业,成员可以正常游戏")
                        UserModel.instance.nowClub.state = ClubConfig.Club_State_Type.Normal
                        this.refresh();
                    })
                }
            }, ViewCheck.Dialog_Type.All)
        })

        let btnSave: cc.Node = this.findChild(this.node, "btnSave")
        UIHelper.onAddEvent(btnSave, () => {
            this.onClickSave()
        })
        let btnRelease: cc.Node = this.findChild(this.node, "btnRelease")
        UIHelper.onAddEvent(btnRelease, () => {
            UIHelper.showCheck("亲友圈解散后无法恢复，请谨慎操作。确定要解散吗?", (isSuc: boolean) => {
                if (isSuc) {
                    ClubSocketClient.req_club_delete(UserModel.instance.nowClub.clubid, () => {
                        UIManager.instance.closeAllView()
                        UIManager.instance.showView(ViewConfig.viewConfig.ViewClubList)
                    })
                }
            }, ViewCheck.Dialog_Type.All)
        })
        this.refresh()
    }

    private onClickSave() {
        Logger.log("onClickSave22==", this.getToggleContainerIndex(this.limit_joinToggle))
        let seemember: number = 1
        if (this.toggleSeemeber.isChecked) {
            seemember = 0;
        }
        let seerank: number = this.getToggleContainerIndex(this.limit_seerank) 
      
        let limit_join: number = this.getToggleContainerIndex(this.limit_joinToggle) + 1
        ClubSocketClient.req_update_club_setting(UserModel.instance.nowClub.clubid, seemember, seerank, limit_join, () => {
            this.refresh();
        })
    }

    private refresh() {
        Logger.log("refresh==", UserModel.instance.nowClub)
        if (UserModel.instance.nowClub.state == ClubConfig.Club_State_Type.dayang) {
            this.btnDayang.active = false
            this.btnKaiye.active = true
        } else {
            this.btnDayang.active = true
            this.btnKaiye.active = false
        }

        if (UserModel.instance.nowClub.seemember == ClubConfig.seemember_type.yes) {
            this.toggleSeemeber.isChecked = false
        } else {
            this.toggleSeemeber.isChecked = true
        }
        if (UserModel.instance.nowClub.seerank == ClubConfig.seerank_type.quanzhu) {
            this.setToggleContainerIndex(this.limit_seerank, 0)
        } else if(UserModel.instance.nowClub.seerank == ClubConfig.seerank_type.quanzhuguanli){
            this.setToggleContainerIndex(this.limit_seerank, 1)
        }else{
            this.setToggleContainerIndex(this.limit_seerank, 2)  
        }
        if (UserModel.instance.nowClub.limit_join == ClubConfig.limit_join_type.all) {
            this.setToggleContainerIndex(this.limit_joinToggle, 0)
        } else if (UserModel.instance.nowClub.limit_join == ClubConfig.limit_join_type.wechat) {
            this.setToggleContainerIndex(this.limit_joinToggle, 1)
        } else if (UserModel.instance.nowClub.limit_join == ClubConfig.limit_join_type.account) {
            this.setToggleContainerIndex(this.limit_joinToggle, 2)
        }

    }
}
